package com.kun.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.Role;
import com.kun.rental.service.IPermissionService;
import com.kun.rental.service.IRoleService;
import com.kun.rental.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 * 角色管理
 *
 * @author ikun
 * @since 2024-07-04
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/rental/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;


    @PostMapping("{start}/{size}")
    public Result search(@PathVariable Integer start,
                         @PathVariable Integer size,
                         @RequestBody Role role) {

        Page<Role> page = new Page<>(start, size);
        return Result.success(roleService.selectList(page, role));
    }


    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:role:add')")   // 权限控制
    public Result save(@RequestBody Role role) {
        return roleService.save(role)?Result.success() : Result.fail();
    }


    /**
     * 修改角色
     * @param role
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        return roleService.updateById(role)?Result.success() : Result.fail();
    }


    /**
     * 判断角色是否有关联的用户
     * @param id
     * @return
     */
    @GetMapping("/hasUser/{id}")
    public Result hasUser(@PathVariable Integer id){
        return roleService.hasUser(id)?Result.success().setMessage("有") : Result.success().setMessage("无");
    }

    /**
     * 删除角色
     * 先判断是否有关联的用户，如果有则不能删除，否则可以删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids) {
        return roleService.delete(ids)?Result.success() : Result.fail();
    }



    //=========================权限分配=======================
    /**
     * 分析：
     * 1.获取菜单树形结构 当前角色id，用户id--做查询处理用户有多少菜单数量
     *      创建一个vo对象，用来存储permission相关信息
     */


    /**
     * 获取菜单树形结构 当前角色id，用户id--做查询处理用户有多少菜单数量
     * @param userId
     * @param roleId
     * @return
     */
    @GetMapping("/permissionTree")
    public Result selectRolePermissionTree( Integer userId, Integer roleId) {
        return Result.success(permissionService.selectPermissionTree(userId,roleId));
    }


    @GetMapping("{roleId}/{permissionIds}")
    public Result assignPermission(@PathVariable int roleId,
                                   @PathVariable String permissionIds) {
        List<Integer> list = Arrays.stream(permissionIds.split(","))
                .map(Integer::parseInt)
                .toList();
        return roleService.assignPermission(roleId, list) ?
                Result.success() : Result.fail();

    }


    /**
     * 不分页获取角色列表
     * @return
     */
    @GetMapping
    public Result list() {
        return Result.success(roleService.list());
    }



}
